package com.lpfy.dtxc;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.tencent.dtxc.R;
import com.lpfy.dtxc.utils.DeviceUtil;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

/**
 * @author: hanGuangHui
 * @data: 2024/2/27
 * @description: 启动页
 */
public class SplashActivity extends AppCompatActivity {

    private TextView tvSeri;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initImmersionBar();
        tvSeri = findViewById(R.id.tv_seri);
        requestPermission();
        String serial = DeviceUtil.getSerial(this);
        if (!TextUtils.isEmpty(serial)) {
            tvSeri.setText("设备序列号: " + serial);
        }
    }

    private void requestPermission() {
        AndPermission.with(this)
                .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE, Permission.READ_PHONE_STATE)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        findViewById(R.id.cl_parent).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }, 5000);
                    }
                }).onDenied(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        findViewById(R.id.cl_parent).postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                    if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
                                            && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                                            && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                                        Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                                        startActivity(intent);
                                    }
                                    finish();
                                }else{
                                    finish();
                                }
                            }
                        }, 5000);
                    }
                }).start();
    }

    protected void initImmersionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(getResources().getColor(R.color.pureWhite));
        }
        //设置共同沉浸式样式
        ImmersionBar.with(this)
//                .transparentStatusBar()
                .statusBarDarkFont(true)
//                .navigationBarColor(R.color.colorPrimary).
//                statusBarColor(R.color.bg).
//                navigationBarColor(R.color.bg).
                .init();
    }
}
